package com.ruoyi.sale.controller;

import java.text.SimpleDateFormat;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.utils.PropertiesDailyGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sale.domain.SaleConfig;
import com.ruoyi.sale.service.ISaleConfigService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 期数配置Controller
 *
 * @author szm
 * @date 2025-10-30
 */
@Api(tags = "期数配置接口")
@RestController
@RequestMapping("/sale/SaleConfig")
public class SaleConfigController extends BaseController {
    @Autowired
    private ISaleConfigService saleConfigService;

    /**
     * 查询期数配置列表
     */
    @ApiOperation("查询期数配置列表")
    @GetMapping("/list")
    public TableDataInfo list(SaleConfig entity, HttpServletRequest req) {
        startPage();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.setEntity(entity);
        List<SaleConfig> list = saleConfigService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 导出期数配置列表
     */
    @ApiOperation("导出期数配置列表")
    @Log(title = "期数配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SaleConfig entity) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.setEntity(entity);
        List<SaleConfig> list = saleConfigService.list(wrapper);
        ExcelUtil<SaleConfig> util = new ExcelUtil<SaleConfig>(SaleConfig. class);
        util.exportExcel(response, list, "期数配置数据");
    }

    /**
     * 获取期数配置详细信息
     */
    @ApiOperation("获取期数配置详细信息")
    @PreAuthorize("@ss.hasPermi('sale:SaleConfig:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getById(@PathVariable("id") String id) {
        return success(saleConfigService.getById(id));
    }

    /**
     * 新增期数配置
     */
    @ApiOperation("新增期数配置")
    @Log(title = "期数配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SaleConfig entity) {

        if (entity.getDailyDate() != null){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            PropertiesDailyGenerator.generateUpadteStage(sdf.format(entity.getDailyDate()),entity.getDailyStage());
        }

        return toAjax(saleConfigService.saveOrUpdate(entity));
    }

    /**
     * 修改期数配置
     */
    @ApiOperation("修改期数配置")
    @Log(title = "期数配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody SaleConfig entity) {
        return toAjax(saleConfigService.updateById(entity));
    }

    /**
     * 删除期数配置
     */
    @ApiOperation("删除期数配置")
    @Log(title = "期数配置", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public AjaxResult remove(@PathVariable String ids) {
        try {
            boolean b = saleConfigService.removeById(ids);
            return toAjax(b);
        }catch (Exception e){
            e.printStackTrace();
        }
        return toAjax(true);
    }
}
